<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\Quality;

use App\Models\Quality\QComplaint;
use Illuminate\Support\Facades\Artisan;
class ComplaintAjax {

    public function checkContent(){
        return view('quality.complaint.check-modal');
    }

    /**
     * @param $content
     * @return \Illuminate\Http\JsonResponse
     */
    public function doCheckContent($content){
        try{
            $content = str_replace(["\r","\n","\r\n"],'',trim($content));
            $count = QComplaint::query()->where('content',$content)->count();
            if($count > 0){
                return response()->json(['status' => true, 'message' => '反馈内容已存在']);
            }else{
                return response()->json(['status' =>false, 'message' => '反馈内容不存在']);
            }
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常，稍后检测']);
        }
    }

    public function calculateComplaint(){
        try{
            Artisan::call('quality:match-complaint');
            return response()->json(['status' => true, 'message' => '操作成功！']);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    public function editTag($id){
        $qc = QComplaint::query()->find($id);
        return view('quality.complaint.edit-tag-modal',compact('id','qc'));
    }

    public function doEditTag($param){
        try{
            $qc = QComplaint::query()->find($param['id']);
            if(!$qc)return response()->json(['status' =>false, 'message' => '反馈记录不存在']);
            $qc->tag = $param['tag'] ?? null;
            $flag = $qc->save();
            if($flag){
                return response()->json(['status' => true, 'message' => '提交成功']);
            }else{
                return response()->json(['status' =>false, 'message' => '提交失败']);
            }
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常，稍后再试']);
        }
    }
}
